package cn.boyu.service.impl;

import cn.boyu.entity.AssetsAmmeter;

import cn.boyu.entity.GatherAmmeter;
import cn.boyu.entity.SysDictData;
import cn.boyu.exception.LogicException;
import cn.boyu.mapper.AssetsAmmeterMapper;
import cn.boyu.query.AmmeterQuery;
import cn.boyu.service.IAssetsAmmeterService;
import cn.boyu.service.IGatherAmmeterService;
import cn.boyu.service.ISysDictDataService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yn
 * @since 2022-11-22
 */
@Service
public class AssetsAmmeterServiceImpl extends ServiceImpl<AssetsAmmeterMapper, AssetsAmmeter>
        implements IAssetsAmmeterService {
    @Autowired
    private AssetsAmmeterMapper ammeterMapper;


    @Autowired
    private ISysDictDataService dataService;

    @Override
    public IPage<AssetsAmmeter> queryPage(AmmeterQuery qo) {
        IPage<AssetsAmmeter> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        QueryWrapper<AssetsAmmeter> qw = new QueryWrapper<>();
        qw.eq(qo.getAmmeterId() != null, "ammeter_id", qo.getAmmeterId());
//        qw.eq(qo.getAmmeterId() != null, "batch_number", qo.getAmmeterId());
        qw.eq("del_flg", 0);

        IPage<AssetsAmmeter> result = page(page, qw);
        for (AssetsAmmeter ammeter : result.getRecords()) {
            List<SysDictData> companyType = dataService.getType("companyType", ammeter.getCompanyType());
            List<SysDictData> manageCompany = dataService.getType("manageCompany", ammeter.getManageCompany());
            ammeter.setCompanyTypeString(companyType.get(0).getDictLabel());
            ammeter.setManageCompanyString(manageCompany.get(0).getDictLabel());
        }
        return result;
    }

    @Override
    public void saveAssets(Long firstId, Long lastId, AssetsAmmeter assetsAmmeter) {
        if (firstId != null && lastId != null && firstId <= lastId) {
            for (long i = firstId; i <= lastId; i++) {
                assetsAmmeter.setAmmeterId(i);
                save(assetsAmmeter);
            }
        } else {
            throw new LogicException("电表号不符合规范！");
        }
    }

    @Override
    public void batchDelete(Long[] ids) {
        ammeterMapper.batchDelete(ids);
    }

    @Override
    public List<AssetsAmmeter> ammeterList(Long gatherId) {

        return ammeterMapper.ammeterList(gatherId);
    }

    @Override
    public List<AssetsAmmeter> mountAmmeterList(Long ammeterId) {
        QueryWrapper<AssetsAmmeter> qw = new QueryWrapper<>();
        qw.eq("del_flg", 0);
        qw.eq(ammeterId != null, "ammeter_id", ammeterId);
        qw.isNull("customer_id");
        return ammeterMapper.selectList(qw);
    }
}
